package com.jic.member.impl;


import cn.hutool.poi.excel.sax.Excel03SaxReader;
import cn.hutool.poi.excel.sax.Excel07SaxReader;
import cn.hutool.poi.excel.sax.handler.RowHandler;
import com.jic.common.base.vo.AdminOperate;
import com.jic.common.base.vo.PageResult;
import com.jic.common.base.vo.RestResult;
import com.jic.member.entity.MemberRoster;
import com.jic.member.entity.MemberType;
import com.jic.member.enums.EXCEPTION;
import com.jic.member.mapper.MemMemberRosterMapper;
import com.jic.member.request.MemMemberRosterBatchRequest;
import com.jic.member.request.MemMemberRosterRequest;
import com.jic.member.service.MemMemberRosterService;
import com.jic.member.service.MemberTypeService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.io.FileInputStream;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;


@Service
@Slf4j
public class MemMemberRosterImpl implements MemMemberRosterService {

    @Resource
    private MemMemberRosterMapper memMemberRosterMapper;

    @Resource
    MemberTypeService memberTypeService;

    @Override
    public RestResult insert(MemMemberRosterRequest memMemberRosterRequest) {
        RestResult result = new RestResult();

        MemberRoster memberRoster = new MemberRoster();

        BeanUtils.copyProperties(memMemberRosterRequest,memberRoster);
        memberRoster.setDeleteFlag(0);
        memberRoster.setDeptId(memMemberRosterRequest.getAdminOperate().getDeptId());
        memberRoster.setCreator( memMemberRosterRequest.getAdminOperate().getOperator());
        memberRoster.setCreatorName(memMemberRosterRequest.getAdminOperate().getOperateName());
        memberRoster.setCreateTime(new Date());
        memberRoster.setUpdateTime(new Date());
        final long insert = memMemberRosterMapper.insert(memberRoster);
        if(insert > 0){
            result.setCode(EXCEPTION.SUCCESS.getName());
        }
        return result;
    }

    @Override
    public RestResult insert(MemMemberRosterBatchRequest request) {
        RestResult result = new RestResult();
        AdminOperate adminOperate = request.getAdminOperate();
        request.getRequests().forEach(memMemberRosterRequest->{
            MemberRoster memberRoster = new MemberRoster();
            BeanUtils.copyProperties(memMemberRosterRequest,memberRoster);
            memberRoster.setDeleteFlag(0);
            memberRoster.setDeptId(adminOperate.getDeptId());
            memberRoster.setCreator( adminOperate.getOperator());
            memberRoster.setCreatorName(adminOperate.getOperateName());
            memberRoster.setCreateTime(new Date());
            memberRoster.setUpdateTime(new Date());

            memMemberRosterMapper.insert(memberRoster);
        });
        result.setCode(EXCEPTION.SUCCESS.getName());
        return result;
    }

    @Override
    public long delete(Long id) {
        return memMemberRosterMapper.delete(id);
    }

    @Override
    public long update(MemMemberRosterRequest memMemberRosterRequest) {
        MemberRoster memberRoster = new MemberRoster();

        BeanUtils.copyProperties(memMemberRosterRequest,memberRoster);
        memberRoster.setUpdateTime(new Date());
        memberRoster.setUpdater(memMemberRosterRequest.getAdminOperate().getOperator());
        memberRoster.setUpdaterName(memMemberRosterRequest.getAdminOperate().getOperateName());
        final long update = memMemberRosterMapper.update(memberRoster);
        return update;
    }

    @Override
    public MemberRoster queryByPrimaryKey(Long id) {
        return memMemberRosterMapper.queryByPrimaryKey(id);
    }

    @Override
    public List<MemberRoster> query(MemberRoster memberRoster) {
        return memMemberRosterMapper.query(memberRoster);
    }

    @Override
    public RestResult<PageResult<MemberRoster>> queryPage(MemMemberRosterRequest memMemberRosterRequest) {

        //计算下标
        int startIndex = (memMemberRosterRequest.getStart() - 1) * memMemberRosterRequest.getLimit();

        MemberRoster memberRoster = new MemberRoster();

        BeanUtils.copyProperties(memMemberRosterRequest,memberRoster);
        memberRoster.setDeptId(memMemberRosterRequest.getAdminOperate().getDeptId());

        List<MemberRoster> list = memMemberRosterMapper.queryPage(startIndex,memMemberRosterRequest.getLimit(),memberRoster);
        if(list.size()>0){
            list.forEach(a->{
                MemberType type = memberTypeService.queryByPrimaryKey(a.getGroupId());
                if(type!=null){
                    a.setGroupName(type.getMemberTypeName());
                }
            });
        }
        long count = memMemberRosterMapper.queryPageCount(memberRoster);
        PageResult<MemberRoster> pageResult = new PageResult<>();
        pageResult.setRows(list);
        pageResult.setTotal(count);
        return RestResult.success(pageResult);
    }

    @Override
    public List<String> importExcel(String fileName , FileInputStream inputStream) {

        List<String> importList = new ArrayList<>();

        RowHandler handler = new RowHandler() {
            @Override
            public void handle(int sheetIndex, int rowIndex, List<Object> rowlist) {

                if(rowIndex !=0){
                    importList.add(rowlist.get(0)+"");
                }
            }
        };
        if(fileName.contains(".xlsx")){

            Excel07SaxReader reader = new Excel07SaxReader(handler);
            reader.read(inputStream, 0);
        }else {
            Excel03SaxReader reader = new Excel03SaxReader(handler);
            reader.read(inputStream, 0);
        }

        return importList;
    }
}
